import {
  getInfo,
  carousel
} from '@/api/lesson';

export default {
  namespaced: true,
  state: {
    // 当前课程信息
    currentLesson: undefined,

    // 轮播图列表
    carouselList: [],

  },
  mutations: {
    UPDATE_CURRENT_LESSON(state, lesson) {
      return state.currentLesson = lesson;
    },
    UPDATE_CAROUSEL_LIST(state, list) {
      return state.carouselList = list;
    }
  },
  actions: {
    getLessonInfo({
      commit
    }, data) {
      return new Promise((resolve, reject) => {
        getInfo({
          lessonId: data
        }).then(res => {
          commit('UPDATE_CURRENT_LESSON', res)
          resolve(res);
        }).catch(error => {
          reject(error);
        })
      })
    },

    getCarousel({
      commit
    }) {
      return new Promise((resolve, reject) => {
        carousel().then(res => {
          commit('UPDATE_CAROUSEL_LIST', res)
          resolve(res);
        }).catch(error => {
          reject(error);
        })
      })
    },

    setCurrentLesson({ commit }, data) {
      return new Promise((resolve, reject) => {
        commit('UPDATE_CURRENT_LESSON', data)
        resolve();
      })
    },
  },
  getters: {
    carouselList(state) {
      return state.carouselList;
    },
    currentLesson(state) {
      return state.currentLesson;
    }
  }
}